package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.*;
import com.project.mapper.*;
import com.project.service.IBuyCarService;
import com.project.service.IBuyOrderService;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private IBuyOrderMapper buyOrderMapper;
    @Autowired
    private IBuyCarMapper buyCarMapper;
    @Autowired
    private IBuyInfoMapper buyInfoMapper;
    @Autowired
    private ISellOrderMapper sellOrderMapper;
    @Autowired
    private ISellCarMapper sellCarMapper;
    @Autowired
    private ISellInfoMapper sellInfoMapper;
    @Override
    public IPage<ProductBean> findByItem(Integer pageNO, String name, Integer startNum, Integer endNum,String unsurePrice, String type) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (name !=null && name.length() !=0){
            qw.like("p_name",name);
        }
        if (startNum !=null){
            qw.ge("p_num",startNum);
        }
        if (endNum !=null){
            qw.le("p_num",endNum);
        }
        if (unsurePrice !=null && unsurePrice.length() !=0){
            if ("未定销售价".equals(unsurePrice)){
              qw.isNull("p_outPrice");
            }
        }
        if (type != null && type.length() !=0){
            if (!"不限".equals(type)){
                qw.eq("p_type",type);
            }
        }
        return productMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public ProductBean findById(String productId) {

        return productMapper.selectById(productId);
    }

    @Override
    public void update(String productId, Integer outPrice) {
        ProductBean productBean = productMapper.selectById(productId);
        productBean.setOutPrice(outPrice);
        productMapper.updateById(productBean);
    }

    @Override
    public IPage<ProductBean> findByType(Integer pageNO, String name, String type) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (name !=null && name.length() !=0){
            qw.like("p_name",name);
        }
        if (type !=null && type.length() !=0 && !"不限".equals(type)){
            qw.eq("p_type",type);
        }
        return productMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public void inset(String supplierName, UserBean loginUser) {
        //完成采购信息的添加（供应商名称，采购员）
        BuyOrderBean buyOrderBean = new BuyOrderBean(supplierName,loginUser.getRealName());
        buyOrderMapper.insert(buyOrderBean);
        //根据登录用户id，查询采购车集合
        QueryWrapper<BuyCarBean> qw =new QueryWrapper<>();
        qw.eq("fk_userId",loginUser.getUserId());
        List<BuyCarBean> buyCarBeanList = buyCarMapper.selectList(qw);

        for (int i=0; i< buyCarBeanList.size();i++){
            BuyCarBean buyCarBean=buyCarBeanList.get(i);
            //添加采购信息明细
            BuyInfoBean buyInfoBean = new BuyInfoBean(buyOrderBean.getBuyOrderId(),buyCarBean.getProductId(),buyCarBean.getPrice(),buyCarBean.getNum());
            buyInfoMapper.insert(buyInfoBean);

            //时修改库存商品的最新采购价，累加商品库存数量
            ProductBean productBean = productMapper.selectById(buyCarBean.getProductId());
            productBean.setName(productBean.getName() + buyCarBean.getNum());
            productBean.setNewInPrice(buyCarBean.getPrice());
            productMapper.updateById(productBean);
        }
        //按登录用户的id删除采购车信息
        buyCarMapper.delete(qw);
    }

    @Override
    public void outbound(SellOrderBean sellOrder, Integer userId) {
        //完成销售信息添加
        sellOrderMapper.insert(sellOrder);
        //按登录用户，查询销售车的集合
        QueryWrapper<SellCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        List<SellCarBean> sellCarBeanList = sellCarMapper.selectList(qw);

        for (int i=0 ; i< sellCarBeanList.size(); i++){
            SellCarBean sellCarBean = sellCarBeanList.get(i);

            ProductBean productBean = productMapper.selectById(sellCarBean.getProductId());
            if (sellCarBean.getNum() > productBean.getNum()){
                sellCarBean.setNum(productBean.getNum());
            }
            sellInfoMapper.insert(new SellInfoBean(productBean.getOutPrice(),sellCarBean.getNum(),sellCarBean.getProductId(),sellOrder.getSellOrderId()));

            //累减库存数量
            productBean.setNum(productBean.getNum() - sellCarBean.getNum());
            productMapper.updateById(productBean);
        }
        //按用户id删除销售车记录
        sellCarMapper.delete(qw);
    }
}
